﻿using Bilibili.Common.Api;
using Bilibili.Model.Resource;
using Bilibili.Model.User;
using PCLUntils.Json;
using PCLUntils.Objects;
using System;
using System.Threading.Tasks;

namespace Bilibili.WebClient.Api
{
    public sealed class HistoryClient : ClientBase
    {
        public async Task<ReturnModel<History>> GetHistory(int pageNum)
        {
            try
            {
                ApiInfo apiInfo = new ApiInfo("http://api.bilibili.com/x/v2/history", $"&pn={pageNum}&ps=20", true, ApiProviderBase.Build6);
                string results = await ApiClient.GetResults(apiInfo);
                History model = results.ParseObject<History>();
                return BuildSuccessResult(model);
            }
            catch (Exception ex)
            {
                return BuildExceptionResult<History>(ex);
            }
        }
        public async Task<ReturnModel> Clear(string id)
        {
            try
            {
                ApiInfo apiInfo = new ApiInfo("http://api.bilibili.com/x/v2/history/delete", $"&kid={id}", true, ApiProviderBase.Build6);
                var results = await ApiClient.PostResults(apiInfo, string.Empty);
                var json = results.ToJObject();
                if (json.IsNotEmpty())
                {
                    if (json["code"].ToInt32() == 0)
                        return BuildSuccessResult();
                    else
                        return BuildFailedResult(json["message"].ToString());
                }
                else
                    return BuildFailedResult();
            }
            catch (Exception ex)
            {
                return BuildExceptionResult(ex);
            }
        }
        public async Task<ReturnModel> ClearAll()
        {
            try
            {
                ApiInfo apiInfo = new ApiInfo("http://api.bilibili.com/x/v2/history/clear", true, ApiProviderBase.Build4);
                var results = await ApiClient.PostResults(apiInfo, string.Empty);
                var json = results.ToJObject();
                if (json.IsNotEmpty())
                {
                    if (json["code"].ToInt32() == 0)
                        return BuildSuccessResult();
                    else
                        return BuildFailedResult(json["message"].ToString());
                }
                else
                    return BuildFailedResult();
            }
            catch (Exception ex)
            {
                return BuildExceptionResult(ex);
            }
        }
    }
}